Git and GitHub
Git is a distributed version control system (DVCS) designed to handle everything from small to very large projects with speed and efficiency. It allows multiple people to collaborate on projects and keep track of changes made to files over time.
Initialize a Repository:
git init
Add Files to the Staging Area:
git add <file1> <file2> ...
Commit Changes:
git commit -m "Commit message"
Create a Branch:
git checkout -b new-feature
Make Changes and Commit:
# Make changes to files
git add .
git commit -m "Implement new feature"
Switch Branches:
git checkout main
Merge Branches:
git merge new-feature
Push Changes to Remote Repository:
git push origin main
Let's say you're working on a project with a friend. You both clone the repository to your local machines:
git clone <repository_url>
cd <repository_name>
You create a new branch to work on a feature:
git checkout -b new-feature
You make changes to some files, add them to the staging area, and commit your changes:
# Make changes
git add .
git commit -m "Implemented new feature"
Meanwhile, your friend makes some changes on the main branch. You want to incorporate those changes into your branch, so you switch back to the main branch, pull the latest changes, and then switch back to your feature branch:
git checkout main
git pull origin main
git checkout new-feature
Finally, when you're done with your feature and have tested it thoroughly, you merge it back into the main branch:
git checkout main
git merge new-feature
And push the changes to the remote repository:
git push origin main
Clone -> git clone <URL>
Clone Branch -> git clone -b <Branch_Name> <URL>
View Branches -> git branch
View Current Branch -> git branch --show-current
Stage Files -> git add .
or git add <File_Path>
Commit Files -> git commit -m "<Commit_Message>"
Push Code to Branch -> git push origin <Branch_Name>